/**
 * fulihui.com Inc.
 * Copyright (c) 2015-2016 All Rights Reserved.
 */
package com.fulihui.integral.core.service;

import com.fulihui.integral.core.api.ISConvertService;
import com.fulihui.integral.core.biz.conv.GeneralConv;
import com.fulihui.integral.core.dal.dataobj.ISConvertDetail;
import com.fulihui.integral.core.dal.dataobj.ISConvertDetailExample;
import com.fulihui.integral.core.dal.dataobj.ext.ISConvertDetailExt;
import com.fulihui.integral.core.dal.mapper.ISConvertDetailMapper;
import com.fulihui.integral.core.dal.mapper.ext.ISConvertDetailExtMapper;
import com.fulihui.integral.core.dto.ISConvertDetailCountByConvTypeDTO;
import com.fulihui.integral.core.dto.ISConvertDetailDTO;
import com.fulihui.integral.core.enums.ISAccountType;
import com.fulihui.integral.core.enums.ISConversionType;
import com.fulihui.integral.core.request.ISConversionReq;
import com.fulihui.integral.core.request.ISConvertDetailCountReq;
import com.fulihui.integral.core.request.ISConvertDetailPageReq;
import com.fulihui.integral.core.result.ISConversionResult;
import com.fulihui.integral.core.result.ISConvertDetailCountByConvTypeResult;
import com.fulihui.integral.core.result.ISConvertDetailPageResult;
import org.near.servicesupport.result.ResultBuilder;
import org.near.servicesupport.util.ConditionValidator;
import org.near.toolkit.common.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 积分兑换服务实现
 *
 * @author yunfeng.li
 * @version $Id: v 0.1 2017年05月02日 13:28 yunfeng.li Exp $
 */
@Service("iSConvertService")
public class ISConvertServiceImpl implements ISConvertService {

    @Resource
    private ISConvertDetailMapper iSConvertDetailMapper;

    @Resource
    private ISConvertDetailExtMapper iSConvertDetailExtMapper;

    /**
     * 积分兑换详情
     *
     * @param request
     * @return
     */
    @Override
    public ISConversionResult recordConvertDetail(ISConversionReq request) {
        ISConversionResult result = new ISConversionResult();
        ConditionValidator.validate(request);
        ISConvertDetail detail = new ISConvertDetail();
        detail.setAmount(request.getAmount());
        detail.setConvType(request.getConvType());
        detail.setGoodsId(request.getGoodsId());
        detail.setIsAccountType(StringUtil.isBlank(request.getIsAccountType())
                ? ISAccountType.BASE_JIFEN.getCode() : request.getIsAccountType());
        detail.setJifenAmount(request.getJifenAmount());
        detail.setMemo(request.getMemo());
        detail.setUserId(request.getUserId());
        detail.setGmtCreate(new Date());
        detail.setGmtModifed(new Date());
        int target = iSConvertDetailMapper.insertSelective(detail);
        if (target != 1) {
            result.setErrmsg("record convert detail is fail!");
            return result;
        }
        return ResultBuilder.succ(result.getClass());
    }

    @Override
    public ISConvertDetailCountByConvTypeResult queryCountByConvType(ISConvertDetailCountReq request) {
        List<ISConvertDetailExt> isConvertDetailExts = iSConvertDetailExtMapper.queryCountByConvType(request.getStartTime(), request.getEndTime());
        ISConvertDetailCountByConvTypeResult result = new ISConvertDetailCountByConvTypeResult();
        result.setDtos(GeneralConv.convert2List(isConvertDetailExts, ISConvertDetailCountByConvTypeDTO.class));
        return result;
    }

    @Override
    public ISConvertDetailPageResult queryPageDetailByConvType(ISConvertDetailPageReq request) {
        ConditionValidator.validate(request);
        if (!StringUtil.equals(ISConversionType.MONEY.getCode(),request.getConvType())){
           return new ISConvertDetailPageResult();
        }
        ISConvertDetailExample example = new ISConvertDetailExample();
        ISConvertDetailExample.Criteria criteria = example.createCriteria();
        criteria.andConvTypeEqualTo(request.getConvType());
        if (request.getStartTime()!=null){
            criteria.andGmtCreateGreaterThanOrEqualTo(request.getStartTime());
        }
        if (request.getEndTime()!=null){
            criteria.andGmtCreateLessThanOrEqualTo(request.getEndTime());
        }
        example.setOffset(request.start4Mysql());
        example.setLimit(request.getRows());
        example.setOrderByClause("gmt_create desc");
        long recordCount = iSConvertDetailMapper.countByExample(example);
        List<ISConvertDetail> isConvertDetails = iSConvertDetailMapper.selectByExample(example);
        ISConvertDetailPageResult result = ResultBuilder.succPageResult(ISConvertDetailPageResult.class,
                request.getPage(), request.getRows(), Integer.parseInt(recordCount + ""));
        result.setDetails(GeneralConv.convert2List(isConvertDetails, ISConvertDetailDTO.class));
        return result;
    }
}
